/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package datas;

import beans.Patient;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author havn
 */
public class PatientObj {

    private connector con;

    public PatientObj() {
        con = new connector();
    }

    public List doSearch(String keyword) {
        try {
            List v = new ArrayList();
            CallableStatement call = con.getCon().prepareCall("{call search_patient(?)}");
            call.setString(1, keyword);
            ResultSet rs = call.executeQuery();
            while (rs.next()) {
                Patient p = new Patient();
                p.setId(rs.getInt("id"));
                p.setAge(rs.getInt("age"));
                p.setName(rs.getString("name"));
                p.setUsername(rs.getString("username"));
                v.add(p);
            }
            return v;
        } catch (SQLException ex) {
            Logger.getLogger(StaffObj.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public boolean insert(Patient p) {
        try {
            CallableStatement call = con.getCon().prepareCall("{call insert_patient(?,?,?,?)}");
            call.setString(1, p.getUsername());
            call.setString(2, p.getPassword());
            call.setString(3, p.getName());
            call.setInt(4, p.getAge());
            //ResultSet rs = call.executeQuery();
            if (call.executeUpdate() > 0) {
                return true;
            }else{
                return false;
            }
        } catch (SQLException ex) {
            Logger.getLogger(PatientObj.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
}
